<?php

/*
 * This file is part of the yuki package.
 * Copyright (c) 2011 olamedia <olamedia@gmail.com>
 *
 * Licensed under The MIT License
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * yLogger records log messages in memory.
 *
 * @package yuki
 * @subpackage log
 * @version SVN: $Id: yLogger.php 149 2011-02-20 06:58:24Z olamedia@gmail.com $
 * @revision SVN: $Revision: 149 $
 * @date $Date: 2011-02-20 06:58:24 +0000 (Sun, 20 Feb 2011) $
 * @author olamedia
 * @license http://www.opensource.org/licenses/mit-license.php MIT
 */
class yLogger{
    const
    EMERG = 'emerg', // System is unusable
    ALERT = 'alert', // Immediate action required
    CRIT = 'crit', // Critical conditions
    ERROR = 'error', // Error conditions
    WARNING = 'warn', // Warning conditions
    NOTICE = 'notice', // Normal but significant
    INFO = 'info', // Informational
    DEBUG = 'debug'; // Debug-level messages
    /**
     * Desired level
     * @var int 
     */
    protected $_level = self::INFO;
    /**
     * Defines how many messages should be logged before they are flushed.
     * @var int
     */
    protected $_autoFlush = 10000;
    /**
     * Messages array.
     * @var ArrayObject
     */
    protected $_logs = null;
    /**
     * Count of log messages in memory.
     * @var int
     */
    protected $_logCount = 0;
    /**
     * yLogger instance.
     * @var yLogger
     */
    protected $_instance = null;
    protected function __construct(){
        // make it low-cost to transfer between methods
        $this->_logs = new ArrayObject();
    }
    public static function getInstance(){
        if (self::$_instance === null){
            // Create a new instance
            Log::$_instance = new Log;
            // Flush the logs at shutdown
            register_shutdown_function(array(self::$_instance, 'flush'));
        }
        return self::$_instance;
    }
    /**
     * Sets log level.
     * @param int $level
     * @return yLogger
     */
    public function setLogLevel($level){
        $this->_level = $level;
        return $this;
    }
    /**
     * Gets current log level
     * @return int
     */
    public function getLogLevel(){
        return $this->_level;
    }
    /**
     * Logs a message.
     * @param string $message Message
     * @param int $priority Message priority
     * @return yLogger
     */
    public function log($message, $priority = self::INFO){
        if ($priority > $this->_level){
            return false;
        }
        $this->_logCount++;
        $this->_logs[$this->_logCount] = new yLogMessage($message, $priority);
        if ($this->_autoFlush > 0 && $this->_logCount >= $this->_autoFlush){
            $this->flush();
        }
        return $this;
    }
    /**
     * Removes all recorded messages from the memory.
     */
    public function flush(){
        // @todo Notify log writers

        $this->_logs = new ArrayObject();
        $this->_logCount = 0;
        return $this;
    }
    /**
     * Logs an emerg message.
     * @param tystringpe $message Message
     */
    public function emerg($message){
        $this->log($message, self::EMERG);
    }
    /**
     * Logs an alert message.
     * @param string $message Message
     */
    public function alert($message){
        $this->log($message, self::ALERT);
    }
    /**
     * Logs a critical message.
     * @param string $message Message
     */
    public function crit($message){
        $this->log($message, self::CRIT);
    }
    /**
     * Logs an error message.
     * @param string $message Message
     */
    public function error($message){
        $this->log($message, self::ERROR);
    }
    /**
     * Logs a warning message.
     * @param string $message Message
     */
    public function warning($message){
        $this->log($message, self::WARNING);
    }
    /**
     * Logs a notice message.
     * @param string $message Message
     */
    public function notice($message){
        $this->log($message, self::NOTICE);
    }
    /**
     * Logs a info message.
     * @param string $message Message
     */
    public function info($message){
        $this->log($message, self::INFO);
    }
    /**
     * Logs a debug message.
     * @param string $message Message
     */
    public function debug($message){
        $this->log($message, self::DEBUG);
    }
}

